package com.yiren.manager.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
    /**
     * 判断时间是不是今天
     * @param date
     * @return    是返回true，不是返回false
     */
	public static boolean isToday(Date date) {
        //当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);
        return day.equals(nowDay);
         
    }
	
	/**
	 * 得到下一个月
	 * @param date
	 * @return
	 */
	public static Date getNextMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		return calendar.getTime();
	}
	
	/**
	 * 获取昨天的日期
	 * 
	 * @return
	 */
	public static Date getYestoday() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		Date time = cal.getTime();
		return time;
	}
	
	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param fDate
	 * @param oDate
	 * @return
	 */
	public static int daysOfTwo(Date fDate, Date oDate) {
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(fDate);
		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
		aCalendar.setTime(oDate);
		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
		return day2 - day1;
	}
	
	/**
	 * 在指定日期加上指定的天数
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDays(Date date,Integer days){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(calendar.DATE, days);
		return calendar.getTime();
	}
	

	/**
     * date2比date1多的天数
     * @param date1    
     * @param date2
     * @return    
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
       int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年            
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            
            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
        	System.out.println(day2-day1);
            return day2-day1;
        }
    }
    
    /**
     * 判断日期在不在当前月
     * @param date
     * @return
     */
    public static boolean isInCurrMonth(Date date){
    	SimpleDateFormat df = new SimpleDateFormat("yyyyMM");//设置日期格式
    	String currDate=df.format(new Date());//获取当前日期的年份和月份
    	
    	if(df.format(date).equals(currDate)){
    		return true;
    	}
    	
    	return false;
    }
    
    public static void main(String[] args) throws ParseException {
    	SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
    	
    	System.out.println(isInCurrMonth(df.parse("20181001")));
    	
    	
	}
}
